<template>
  <div class="login">
    <div class="project-title">
      <img src="@/assets/images/logo.png" width="70" height="70" alt="天育">
      <span>内部质量诊断与改进信息平台</span>
    </div>
    <div class="login-wrap">
      <form class="login-form" method="post"  @keyup.enter="login">
        <div class="sub-title">登录</div>
        <div class="form-indent">
          <input type="text" class="username input" v-model.trim="username" placeholder="账号" />
          <input type="password" class="password input" v-model.trim="password" placeholder="密码" />
        </div>
        <!--<input type="submit" value="登录">-->
        <div class="like-button" v-loading="loading" @click="login">立即登录</div>
        <p class="footer">如忘记密码请立即联系管理员</p>
      </form>
    </div>
  </div>
</template>

<script>
import { login } from '../api/system'
import { setStorage, getStorage } from '../config/utils'
import { getUserPermission } from '@/api/systemManage'

export default {
  name: 'login',
  data () {
    return {
      loading: false,
      username: '',
      password: '',
      actionUrl: this.baseURL + '/login/form',
      buttonPermission: [],
      menuList: []
    }
  },
  methods: {
    login () {
      if (!this.username) {
        this.$message.warning('请输入用户名')
        return
      }
      if (!this.password) {
        this.$message.warning('请输入密码')
        return
      }
      // 新建FormData对象
      const formData = new FormData()
      formData.append('username', this.username)
      formData.append('password', this.password)

      login(formData)
        .then(res => {
          console.log(res, 'userinfo');
          setStorage('userInfo', res)
          this.getUserPermission()
        })
        .catch(() => {})
        console.log(login);
    },
    // 获取当前用户权限
    async getUserPermission () {
      await getUserPermission()
        .then(res => {
          if (res && res.length) {
            this.getMenu(res)

            this.$store.dispatch('GET_SYSTEM')
            this.$store.dispatch('GET_RECTIFICATION_RANGE')
          } else {
            this.$message.error('对不起，您没有操作权限，请联系管理员分配权限')
          }
        })
        .catch()
    },
    // 处理菜单
    getMenu (data) {
      const menuList = this.recursiveData(data)
      sessionStorage.setItem('menuList', JSON.stringify(menuList))
      const list = menuList
      if (list.length) {
        // 设置左侧菜单
        this.$store.dispatch('SET_TREE_MENU', list[0].children)

        // 设置顶部选中菜单项
        this.$store.dispatch('SET_ACTIVE_INDEX', list[0].id)

        if (list[0].resUrl) {
          this.$router.push(list[0].resUrl)
        } else {
          if (list[0].children && list[0].children.length) {
            this.$router.push(list[0].children[0].resUrl)
          } else {
            this.$router.push({
              path: '/error'
            })
          }
        }
      } else {
        this.$message.error('对不起，您没有操作权限，请联系管理员分配权限')
      }

      this.getButtonPermission()
    },
    // 递归处理菜单
    recursiveData (list) {
      const data = [...list]
      if (data.length) {
        data.forEach(item => {
          if (item.type == 2) {
            this.buttonPermission.push(item)
            item.children = []
          } else if (item.children && item.children.length) {
            if (item.children[0].type == 2) {
              this.buttonPermission.push(item.children)
              item.children = []
            } else {
              this.recursiveData(item.children)
            }
          }
        })
      }

      return data
    },
    // 处理登录用户按钮权限
    getButtonPermission () {
      const temp = []
      if (this.buttonPermission.length) {
        const arr = this.buttonPermission.flat()
        arr.forEach(item => {
          temp.push(item.id)
        })
      }
      if (temp.length) {
        setStorage('buttonPermission', temp)
      }
    }
  },
  created () {
    // 进入登录页清除所有登录存储在本地的数据
    // sessionStorage.clear()
    // 想清空vuex中的数据，让页面重新加载就行了
    // window.location.reload()
  }
}
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100vh;
  background: url('~@/assets/images/login_bg.png') no-repeat center center / 100% 100%;
  .project-title {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 70px;
    span {
      margin-left: 20px;
      color: #fff;
      font-size: 50px;
      font-weight: 500;
    }
  }
  @media screen and (max-width: 750px) {
    background: #000047;
    span {
      display: none;
    }
  }
}

.login-wrap {
  /*z-index: 99;*/
  /*position: absolute;*/
  /*right: 10%;*/
  /*top: 30%;*/
  margin: 0 10% 0 auto;
  width: 26%;
  max-width: 420px;
  min-width: 360px;
  height: 380px;
  border-radius: 14px;
  box-sizing: border-box;
  background: #000047 url('~@/assets/images/login_box.png') center center / 100% 100%;
  padding: 40px;
  @media screen and (max-width: 750px) {
    margin: 0 auto;
    width: 100%;
  }
  .login-form {
  }
  .sub-title {
    font-size: 22px;
    color: #24b7d8;
    font-weight: 500;
  }
  .input {
    display: block;
    width: 100%;
    padding: 10px 4px 8px;
    background: transparent;
    border: 0 none;
    border-bottom: 1px solid #26c1df;
    box-sizing: border-box;
    font-size: 16px;
    color: #1984b4;
    &::placeholder {
      color: #1984b4;
    }
    &:focus {
      outline: transparent;
      border-bottom: 1px solid #01cce5;
    }
  }
  .username {
    margin-top: 30px;
  }
  .password {
    margin-top: 24px;
  }
  .like-button {
    display: block;
    padding: 10px 16px;
    margin-top: 50px;
    background: #00edff;
    border-radius: 4px;
    color: #ffffff;
    cursor: pointer;
    font-size: 18px;
    text-align: center;
    letter-spacing: 2px;
    &:active {
      opacity: 0.7;
    }
  }

  .footer {
    margin-top: 22px;
    color: #2feeff;
    font-size: 14px;
    text-align: center;
  }
}
</style>
